#region Copyright (c) 2006 Jonathan Nauman
/*       _\|/_
         (o o)
+-----oOO-{_}-OOo-----------------------+
| Copyright (c) Jonathan Naumann        |
+---------------------------------------+
| 
| File: MonoPad.ConfigProvider.cs
| Version:	0.1.2
+
*/
#endregion

//#define __DEBUG

using System;
using System.IO;
using System.Security.Principal;

namespace MonoPad {
	///
	/// Class ConfigProvider
	///
	public class ConfigProvider {
		private ConfigContainer conf = new ConfigContainer();
		
		///
		/// Default constructor
		///
		public ConfigProvider() {
			this._CheckSettingsFile();
		}
		
		///
		/// Get the configuration container
		///
		public ConfigContainer GetConfiguration() {
			this._LoadConfiguration();
			return this.conf;
		}
		
		///
		/// Set (save) the configuration
		///
		public void SetConfiguration(ConfigContainer conf) {
			this._SaveConfiguration(conf);
		}
		
		///
		/// Check if the settings file exists
		///
		private void _CheckSettingsFile() {
			string path = "/home/" + WindowsIdentity.GetCurrent().Name + "/.MonoPad/";
			
			DirectoryInfo info = new DirectoryInfo(path);
			
			if(!info.Exists) {
				info.Create();
			}
			
			FileInfo file = new FileInfo(path + "BaseSettings.xml");
			
			if(!file.Exists) {
				FileStream fs = File.OpenWrite(path + "BaseSettings.xml");
				StreamWriter swOutput = new StreamWriter(fs);
				
				swOutput.Write(""
				+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
				+ "\t<Configuration>\n"
				+ "\t\t<LineNumbers>1</LineNumbers>\n"
				+ "\t\t<MonotypeFont>1</MonotypeFont>\n"
				+ "\t\t<TabWidth>4</TabWidth>\n"
				+ "\t</Configuration>");
				swOutput.Flush();
				
				swOutput.Close();
				fs.Close();
			}
		}
		
		///
		/// [PRIVATE] Load the configuration
		///
		private void _LoadConfiguration() {
			string s = "";
			
			// current identity
			conf.UserName = WindowsIdentity.GetCurrent().Name;
			conf.HomeDirectory = "/home/" + WindowsIdentity.GetCurrent().Name + "/";
			conf.ConfigPath = conf.HomeDirectory + ".MonoPad/";
			conf.ConfigFile = conf.ConfigPath + "BaseSettings.xml";
			
			#if __DEBUG
				Console.WriteLine("Current user: " + conf.UserName);
				Console.WriteLine("Current home directory: " + conf.HomeDirectory);
				Console.WriteLine("Current config directory: " + conf.ConfigPath);
				Console.WriteLine("Current config file: " + conf.ConfigFile);
			#endif
			
			// config file
			DirectoryInfo di = new DirectoryInfo(conf.ConfigPath);
			FileInfo fi = new FileInfo(conf.ConfigFile);
			
			if(!di.Exists)
				di.Create();
			
			if(fi.Exists) {
				XmlProvider xml = new XmlProvider(conf.ConfigFile);
				
				try {
					conf.LineNumbers = Convert.ToBoolean(Convert.ToInt32(xml.ReadTag("LineNumbers")));
				}
				catch(Exception ex) {
					s = ex.Message;
					conf.LineNumbers = false;
				}
				
				try {
					conf.MonotypeFont = Convert.ToBoolean(Convert.ToInt32(xml.ReadTag("MonotypeFont")));
				}
				catch(Exception ex) {
					s = ex.Message;
					conf.MonotypeFont = false;
				}
				
				try {
					conf.TabWidth = Convert.ToUInt32(xml.ReadTag("TabWidth"));
				}
				catch(Exception ex) {
					s = ex.Message;
					conf.TabWidth = 4;
				}
				
				xml = null;
			}
			else {
				fi.Create();
				
				conf.LineNumbers = false;
				conf.MonotypeFont = false;
				conf.TabWidth = 4;
			}
			
			#if __DEBUG
				Console.WriteLine("Show linenumbers: " + conf.LineNumbers.ToString());
				Console.WriteLine("Use monotype font: " + conf.MonotypeFont.ToString());
				Console.WriteLine("Tab width: " + conf.TabWidth.ToString());
			#endif
		}
		
		///
		/// [PRIVATE] Save the configuration
		///
		private void _SaveConfiguration(ConfigContainer conf) {
			try {
				XmlProvider xml = new XmlProvider(conf.ConfigFile, OpenMode.ForWriting);
				
				xml.WriteRootStartTag("Configuration");
				xml.WriteTag("LineNumbers", ( conf.LineNumbers ? "1" : "0" ));
				xml.WriteTag("MonotypeFont", ( conf.MonotypeFont ? "1" : "0" ));
				xml.WriteTag("TabWidth", conf.TabWidth.ToString());
				xml.WriteRootEndTag();
				
				xml.ExplicitCloseWriter();
			}
			catch(Exception ex) {
				Console.WriteLine("ERROR: " + ex.Message);
			}
			
			#if __DEBUG
				Console.WriteLine("Configuration saved ...");
			#endif
		}
	}
}
